package com.bye.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bye.dto.CustomerDto;
import com.bye.mapper.CustomerMapper;
import com.bye.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

@Service
public class CustomerServiceImpl implements CustomerService {

    @Autowired
    private CustomerMapper customerMapper;

    @Override
    public String countCustomer() {
        return   customerMapper.selectCount(null).toString();
    }

    @Override
    public List<CustomerDto> selectCustomersOfConsume() {
        return customerMapper.selectCustomersOfConsume();
    }

    @Override
    public IPage<CustomerDto> showCustomer(CustomerDto customerDto) {
        Page<CustomerDto> page = new Page<>(customerDto.getCurrentPage(), customerDto.getPageSize());
        return  customerMapper.selectPage(page,null);
    }

    @Override
    public int changeStatus(CustomerDto customerDto) {
        QueryWrapper qr = new QueryWrapper();
        qr.eq("id", customerDto.getId());
        return customerMapper.update(customerDto, qr);

    }

    @Override
    public List<CustomerDto> selectCustomerOfPhone(CustomerDto customerDto) {
        QueryWrapper<CustomerDto> queryWrapper = new QueryWrapper<>();
        if (customerDto.getPhoneCustomer() != null && !customerDto.getPhoneCustomer().isEmpty()) {
            // 如果手机号不为空，使用手机号查询
            queryWrapper.eq("phone_customer",customerDto.getPhoneCustomer());
        } else if (customerDto.getStartTime() != null && customerDto.getEndTime() != null) {
            // 如果手机号为空，使用时间范围查询
            queryWrapper.between("join_time", customerDto.getStartTime(), customerDto.getEndTime());
        }
        return customerMapper.selectList(queryWrapper);
    }

}
